package com.edoomath.api.web.controller.mailtask;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.demo2do.core.entity.exception.BusinessException;
import com.demo2do.core.entity.result.HttpResult;
import com.edoomath.api.servcice.MatrialService;
import com.edoomath.api.servcice.UserService;
import com.edoomath.entity.user.User;

/**
 * 
 * @author mohanlan
 *
 */
@Controller
@RequestMapping("/mail-tasks")
public class MailTaskController {
    
    @Autowired
    private MatrialService mailTaskService;
    
    @Autowired
    private UserService userService;
    
    @RequestMapping(value = "/validate", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public HttpResult validate(@RequestParam(value = "unionid") String unionid,
    						   @RequestParam("materialId") List<Long> materialIds,
    						   @RequestParam(value = "extraMaterialId", required = false) List<Long> extraMaterialIds) {
    	HttpResult httpResult = new HttpResult();
    	try {
    		
    		User user = userService.load(unionid);
    		if(user.isChildren() && StringUtils.isNotBlank(user.getMobile())) {
        		user = userService.getParent(user);
        		unionid = user.getUnionid();
        	}
        	
        	mailTaskService.validateMailTask(unionid, materialIds, extraMaterialIds);
        	httpResult.success().message("可以发送");
        	
        } catch (BusinessException e) {
        	httpResult.fail("500").message(e.getMessage());
        }
        
        return httpResult;
    }
    
    /**
     * 
     * @param unionid
     * @param email
     * @param materialId
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public HttpResult cretae(@RequestParam(value = "unionid") String unionid,
    						 @RequestParam(value = "email") String email,
    					     @RequestParam("materialId") List<Long> materialIds,
    					     @RequestParam(value = "extraMaterialId", required = false) List<Long> extraMaterialIds) {
        
        HttpResult httpResult = new HttpResult();
        
        try {
        	
        	User user = userService.load(unionid);
    		if(user.isChildren() && StringUtils.isNotBlank(user.getMobile())) {
        		user = userService.getParent(user);
        		unionid = user.getUnionid();
        	}
        	
        	mailTaskService.createMailTask(unionid, email, materialIds, extraMaterialIds);
        	httpResult.success().message("创建成功");
        	
        } catch (BusinessException e) {
        	httpResult.fail("500").message(e.getMessage());
        }
        
        return httpResult;
    }
    
}
